import "./UI.ts";

//游戏参数
let BEGINNING_TIME = 93.313;
const START_POSITION = new GameVector3(206.3, 12, 247.8);
const BLOCK_SCALE = new GameVector3(0.05, 0.05, 0.05);
const CAMERA_OFFSET = new GameVector3(-20, 10, 10);

// 初始化所有非玩家实体
world.querySelectorAll("*").forEach((entity) => {
  if (!entity.player) {
    entity.fixedPosition = entity.position.clone();
    entity.fixedScale = entity.meshScale;
    entity.fixedCollides = entity.collides;
    entity.fixedFixed = entity.fixed;
    if (entity.id.startsWith("正方形赛道")) {
      entity.collides = true;
      entity.fixed = true;
    }
  }
});

// 玩家方块属性调节函数
function setPlayerBlockPhysics(
  entity: any,
  collides: boolean,
  gravity: boolean
) {
  if (entity && entity.block) {
    entity.block.collides = collides;
    entity.block.gravity = gravity;
    entity.block.fixed = false;
  }
}

world.onPlayerJoin(async ({ entity }) => {
  entity.death = false;
  entity.player.colorLUT = "lut/Filmic6.lut";
  entity.control = true;
  entity.cloning = false;
  entity.player.dialog({
    type: GameDialogType.TEXT,
    title: `欢迎`,
    content: `点击空格(跳跃键)开始游戏，如果开始了就可以转向`,
  });
  entity.clicking = 0;
  entity.block = world.createEntity({
    mesh: "mesh/方块-被骗.vb",
    position: new GameVector3(0, 3, 0),
    meshScale: new GameVector3(0.05, 0.05, 0.05),
    collides: false,
    fixed: false,
    gravity: false,
  });
  entity.block.id = `${entity.player.name}的线`;
  entity.block.addTag("block");
  entity.block.showEntityName = true;
  entity.player.cameraMode = GameCameraMode.FIXED;
  entity.player.canFly = false;
  entity.player.enableAction0 = false;
  entity.player.enableAction1 = false;
  entity.player.enableCrouch = false;
  entity.player.enableDoubleJump = false;
  entity.player.cameraPosition = new GameVector3(
    entity.block.position.x - 20,
    entity.block.position.y + 10,
    entity.block.position.z - 10
  );
  entity.player.cameraTarget = entity.block.position.clone();
  entity.Jump = false;
  entity.player.invisible = true;
  entity.player.showName = false;
  entity.player.spectator = false;
  entity.going = "z";
  entity.player.cameraEntity = entity.block;
  entity.player.cameraFovY = 80.5;
  while (1) {
    entity.progress += 0.5;
    await sleep(500);
  }
});

world.onPress(async ({ button, entity }) => {
  if (button === GameButtonType.JUMP && entity.control) {
    if (entity.Jump == false) {
      entity.progress = 0;
      entity.cloning = true;
      entity.Jump = true;
      await sleep(2000);
      entity.player.music.sample = "audio/你被骗了.mp3";
    } else {
      if (entity.going === "x") {
        entity.going = "z";
      } else if (entity.going == "z") {
        entity.going = "x";
      }
    }
    console.log(
      "第" +
      entity.clicking +
      "次点击，坐标:(" +
      Math.floor(entity.block.position.x * 100) / 100 +
      ", " +
      Math.floor(entity.block.position.y * 100) / 100 +
      ", " +
      Math.floor(entity.block.position.z * 100) / 100 +
      ")"
    );
    entity.clicking += 1;
  }
});

world.onTick(() => {
  world.querySelectorAll("player").forEach(async (e) => {
    e.alwaysPercent = `${Math.ceil((e.progress / BEGINNING_TIME) * 100)}%`;
    // if (e.alwaysPercent == `60%`) {
    //     console.log('60%')
    // }
    if (e.Jump) {
      if (e.going === "x") {
        e.block.position.x += 1;
      } else if (e.going === "z") {
        e.block.position.z += 1;
      }

      if (e.block.position.y <= 1) {
        e.control = false;
        e.percent = `${Math.ceil((e.progress / BEGINNING_TIME) * 100)}%`;
        e.cloning = false;
      }
      if (e.block.position.y <= -6) {
        e.Jump = false;
        e.death = true;
        e.player.colorLUT = "lut/Night1.lut";
        await e.player.dialog({
          type: GameDialogType.SELECT,
          title: `失败`,
          content: `失败！\n当前进度\n${e.percent}\n[当前Demo暂不支持复活]`,
          options: ["重新开始"],
        });

        world.querySelectorAll("*").forEach(async (e) => {
          if (!e.player) {
            e.fixedPosition = e.position.clone();
            e.fixedScale = e.meshScale;
            e.fixedCollides = e.collides;
            e.fixedFixed = e.fixed;
            if (e.id.startsWith("跳舞的线-界外")) {
              e.collides = true;
              e.fixed = true;
            }
          }
        });
        e.going = "x";
        e.progress = 0;
        e.player.colorLUT = "lut/Filmic6.lut";
        e.player.music.sample = "";
        e.control = true;
        e.block.destroy();
        e.cloning = false;
        e.clicking = 0;

        e.block = world.createEntity({
          mesh: "mesh/方块-序幕.vb",
          position: new GameVector3(206.3, 12, 247.8),
          meshScale: new GameVector3(0.05, 0.05, 0.05),
          collides: true,
          fixed: false,
          gravity: true,
        });

        e.player.cameraPosition = new GameVector3(
          e.block.position.x - 20,
          e.block.position.y + 10,
          e.block.position.z - 10
        );
        e.player.cameraTarget = e.block.position.clone();
        e.block.id = e.player.name;
        e.block.addTag("block");
        e.block.showEntityName = true;
        e.death = false;
        world.querySelectorAll(`.${e.player.userId}`).forEach(async (e1) => {
          for (let i = 0; i <= 100; i++) {
            await sleep(10);
          }
          e1.destroy();
        });
      }
      if (e.cloning) {
        e.player.cameraPosition = new GameVector3(
          e.block.position.clone().x - 20,
          e.block.position.y + 10,
          e.block.position.z - 10
        );
        e.player.cameraTarget = e.block.position.clone();
        // let block_body = world.createEntity({
        //   id: "block_body",
        //   position: e.block.position.clone(),
        //   meshScale: new GameVector3(0.00000001, 0.00000001, 0.00000001),
        //   collides: false,
        //   fixed: true,
        //   gravity: false,
        // });
        // if (!block_body) return;
        // block_body.addTag(e.player.userId);
        // block_body.mesh = "mesh/方块.vb";
        // await sleep(125);
        // block_body.meshScale = new GameVector3(0.05, 0.05, 0.05);
        // await sleep(10000);
        // if (e.death === false) {
        //   block_body.destroy();
        // }
      }
    }
  });
});
